package com.zackeus.sipservice;

import android.content.Context;
import android.content.Intent;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.zackeus.common.utils.ObjectUtils;
import com.zackeus.common.utils.StringUtils;
import com.zackeus.sipservice.constant.Action;
import com.zackeus.sipservice.constant.Param;
import com.zackeus.sipservice.entity.CodecPriority;

import java.util.ArrayList;

/**
 * @name: SipServiceCommand
 * @author: zz
 * @date: 2022/9/19
 */
public class SipServiceCommand {

    /**
     * SIP 账号检查
     * @param accountId
     */
    private static void checkAccount(String accountId) {
        if (StringUtils.isBlank(accountId) || !accountId.startsWith("sip:")) {
            throw new IllegalArgumentException("Invalid accountId! Example: sip:user@domain");
        }
    }

    public static String register(@NonNull Context context, @NonNull SipAccountData sipAccount) {
        return register(context, sipAccount, null);
    }

    /**
     * SIP 账号注册
     * @param context
     * @param sipAccount
     * @param codecPriorities 编解码优先级
     * @return
     */
    public static String register(@NonNull Context context, @NonNull SipAccountData sipAccount,
                                  @Nullable ArrayList<CodecPriority> codecPriorities) {
        String accountId = sipAccount.getIdUri();
        checkAccount(accountId);

        Intent intent = new Intent(context, SipService.class);
        intent.setAction(Action.ACTION_SET_ACCOUNT);
        intent.putExtra(Param.PARAM_ACCOUNT_DATA, sipAccount);
        context.startService(intent);

        if (ObjectUtils.isNotEmpty(codecPriorities))
            intent.putParcelableArrayListExtra(Param.PARAM_CODEC_PRIORITIES, codecPriorities);

        return accountId;
    }

    /**
     * SIP 账号注销
     * @param context
     * @param accountId
     */
    public static void unregister(@NonNull Context context, @NonNull String accountId) {
        checkAccount(accountId);

        Intent intent = new Intent(context, SipService.class);
        intent.setAction(Action.ACTION_UNREGISTER_ACCOUNT);
        intent.putExtra(Param.PARAM_ACCOUNT_ID, accountId);
        context.startService(intent);
    }

    /**
     * SIP 账号注销并删除
     * 调用此方法可能不会触发 onRegistrationState 回调
     * @param context
     * @param accountId
     */
    public static void remove(@NonNull Context context, @NonNull String accountId) {
        checkAccount(accountId);

        Intent intent = new Intent(context, SipService.class);
        intent.setAction(Action.ACTION_REMOVE_ACCOUNT);
        intent.putExtra(Param.PARAM_ACCOUNT_ID, accountId);
        context.startService(intent);
    }

    public static void refreshRegistration(@NonNull Context context, @NonNull String accountId){
        refreshRegistration(context, accountId, 0);
    }

    /**
     * 刷新注册
     * @param context
     * @param accountId
     * @param regExpTimeout
     */
    public static void refreshRegistration(@NonNull Context context, @NonNull String accountId, int regExpTimeout){
        checkAccount(accountId);

        Intent intent = new Intent(context, SipService.class);
        intent.setAction(Action.ACTION_REFRESH_REGISTRATION);
        intent.putExtra(Param.PARAM_ACCOUNT_ID, accountId);
        intent.putExtra(Param.PARAM_REG_EXP_TIMEOUT, regExpTimeout);
        context.startService(intent);
    }

    public static void start(@NonNull Context context) {
        start(context, null, null);
    }

    /**
     * 启动 SIP
     * @param context
     * @param iconName 通知图标资源名称
     * @param iconType 通知图标资源类型
     */
    public static void start(@NonNull Context context, @Nullable String iconName, @Nullable String iconType) {
        Intent intent = new Intent(context, SipService.class);
        intent.setAction(Action.ACTION_START_SIP_STACK);
        if (StringUtils.isNotBlank(iconName)) {
            intent.putExtra(Param.PARAM_NOTIFICATION_ICON_NAME, iconName);
        }
        if (StringUtils.isNotBlank(iconType)) {
            intent.putExtra(Param.PARAM_NOTIFICATION_ICON_TYPE, iconType);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(intent);
        } else {
            context.startService(intent);
        }
    }

    /**
     * 关闭SIP
     * @param context
     */
    public static void stop(@NonNull Context context) {
        Intent intent = new Intent(context, SipService.class);
        intent.setAction(Action.ACTION_STOP_SIP_STACK);
        context.startService(intent);
    }

}
